package com.jiantong.lawEnforcing.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.lawEnforcing.impl.dao.EvidencesDao;
import com.jiantong.lawEnforcing.model.Evidences;
import com.jiantong.lawEnforcing.service.EvidencesService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 证物信息
 * Created by mz03 on 2017/8/14.
 */
@Slf4j
@Service
@RpcProvider
public class EvidencesServiceImpl implements EvidencesService {
    private final EvidencesDao evidencesDao;

    @Autowired
    public EvidencesServiceImpl(EvidencesDao evidencesDao) {
        this.evidencesDao = evidencesDao;
    }


    /**
     * id 查找
     *
     * @param id 需要查询的id
     * @return evidences
     */
    @Override
    public Response<Evidences> finById(Long id) {
        try {
            Evidences evidences = evidencesDao.findById(id);
            if (evidences != null) {
                return Response.ok(evidences);
            } else {
                log.error("evidences (id={}) is not found", id);
                return Response.fail("evidences.not.found");
            }
        }catch (Exception e){
            log.error("failed to find evidences(id={}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("evidences.find.fail");
        }
    }

    /**
     * id 查找
     *
     * @param ids 查询的id列表
     * @return evidencesList
     */
    @Override
    public Response<List<Evidences>> finByIds(List<Long> ids) {
        try {
            List<Evidences> evidencesList = evidencesDao.findByIds(ids);
            if (evidencesList != null) {
                return Response.ok(evidencesList);
            } else {
                log.error("evidencesList (ids={}) is not found", ids);
                return Response.fail("evidencesList.not.found");
            }
        }catch (Exception e){
            log.error("failed to find evidencesList(ids={}),cause:{}",ids, Throwables.getStackTraceAsString(e));
            return Response.fail("evidencesList.find.fail");
        }
    }

    /**
     * 创建新闻新单据
     *
     * @param evidences 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Long> create(Evidences evidences) {
        try {
            evidencesDao.create(evidences);
            return Response.ok(evidences.getId());
        }catch (Exception e) {
            log.error("failed.to.evidences.askAnswer:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("evidences.askAnswer.failed");
        }
    }

    /**
     * 更新新闻的单据
     *
     * @param evidences 单据对象
     * @return true or false
     */
    @Override
    public Response<Boolean> update(Evidences evidences) {
        try{
            return Response.ok(evidencesDao.update(evidences));
        }catch (Exception e){
            log.error("failed.to.update.evidences:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("update.evidences.failed");
        }
    }
}
